/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package authentication;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

/**
 *
 * @author tuandn
 */
public class PutMethodDemo {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws Exception {
        DefaultHttpClient httpClient = new DefaultHttpClient();

        UsernamePasswordCredentials admin = new UsernamePasswordCredentials("admin", "admin");
        httpClient.getCredentialsProvider().setCredentials(new AuthScope("localhost", 8080), admin);
        HttpPut putRequest = new HttpPut("http://localhost:8080/rpc/rest/csc/v2/json/users/communityInterests/1");

        StringEntity input;
        input = new StringEntity("<root><communityInterestUpdate><communitiesToBeAdded>1</communitiesToBeAdded><communitiesToBeRemoved>2005</communitiesToBeRemoved></communityInterestUpdate></root>");
        input.setContentType("application/xml");
        
        input = new StringEntity("{'WSCommunities':{'communitiesToBeAdded':[2006,2009,1],'communitiesToBeRemoved':[]}}");
        input.setContentType("application/json");

        putRequest.setEntity(input);
        HttpResponse response = httpClient.execute(putRequest);
        HttpEntity entity = response.getEntity();

        System.out.println("Login form get: " + response.getStatusLine());
        if (entity != null) {
            System.out.println("Json data:" + EntityUtils.toString(entity).trim());
        }
        EntityUtils.consume(entity);
    }
}
